package mytool

import (
	"io/ioutil"
	"log"
	"os"
	"path/filepath"

	"gopkg.in/yaml.v2"
)

var AllConf Config

func init() {
	AllConf = Getconfig()
}

type Config struct {
	Port         string       `yaml:"port"`
	Token        string       `yaml:"token"`
	MachineConf  MachineConf  `yaml:"machineConf"`
	EmailConf    EmailConf    `yaml:"emailConf"`
	DingConf     DingWxConf   `yaml:"dingConf"`
	WxConf       DingWxConf   `yaml:"wxConf"`
	WxVerifyConf WxVerifyConf `yaml:"wxVerifyConf"`
}

type WxVerifyConf struct {
	Token          string `yaml:"token"`
	EncodingAeskey string `yaml:"encodingAeskey"`
	ReceiverId     string `yaml:"receiverId"`
}

type DingWxConf struct {
	Enabled    bool   `yaml:"enabled"`
	Corpid     string `yaml:"corpid"`
	Corpsecret string `yaml:"corpsecret"`
	Agentid    string `yaml:"agentid"`
	Touser     string `yaml:"touser"`
}

type EmailConf struct {
	Enabled  bool   `yaml:"enabled"`
	Smtpaddr string `yaml:"smtpaddr"`
	From     string `yaml:"from"`
	Password string `yaml:"password"`
	Sendto   string `yaml:"sendto"`
}

type MachineConf struct {
	Enabled bool   `yaml:"enabled"`
	Url     string `yaml:"url"`
}

func Getconfig() Config {
	var config Config
	ex, err := os.Executable()
	exPath := filepath.Dir(ex)
	yamlFile, err := ioutil.ReadFile(exPath + "/config.yaml")
	CheckErr(err)
	err = yaml.Unmarshal(yamlFile, &config)
	return config
}

func CheckErr(err error) {
	if err != nil {
		log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime)
		log.Println(err.Error())
	}
}
